package org.skywind.WindSearch.servlet;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.skywind.Robot.interfaces.RobotControl;
import org.skywind.WindSearch.OperationHost;
import org.skywind.WindSearch.SearchManager;
import org.skywind.WindSearch.interfaces.Operation;
import org.skywind.tools.InfoFormat;

public class RemoteScreen extends HttpServlet {
	BufferedImage image;
	/**
	 * Constructor of the object.
	 */
	public RemoteScreen() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Rectangle size = new Rectangle(800, 600);
		try {
			int width = InfoFormat.intByString(request.getParameter("width"));
			int height = InfoFormat.intByString(request.getParameter("height"));
			if (width > 0 && height > 0)
				size.setSize(width, height);
		} catch (Exception e) {
		}
		String host = (String) request.getParameter("host");
		SearchManager sm = (SearchManager) this.getServletContext().getAttribute("system");
		OperationHost oh = (OperationHost) sm.getOprationPool().get(host);
		Object Searcher = oh.getObjectPool().get("Searcher");
		Operation oi = (Operation) Searcher;
		RobotControl rc = (RobotControl) Searcher;
		// PipedInputStream in = new PipedInputStream();
		// PipedOutputStream pos = new PipedOutputStream(in);
		// pos.write();
		// Log.print("[RemoteScreen]host:%s startting...");
		ByteArrayInputStream in = new ByteArrayInputStream(oi
				.cutScreenZipStream(size));
		// ZipInputStream zis = new ZipInputStream(in);
		// ZipEntry ze = zis.getNextEntry();
		// Log.print(String.format("[RemoteScreen]host:%s, name=%s, size=%d,
		// time=%d",host, ze.getName(), ze.getSize(), ze.getTime()));
		image = ImageIO.read(in);
		try {
			int x = Integer.parseInt(request.getParameter("x"));
			int y = Integer.parseInt(request.getParameter("y"));
			rc.mouseMove(x, y);
		} catch (Exception e) {
		}
		try {
			int kp = Integer.parseInt(request.getParameter("kp"));
			rc.keyPress(kp);
		} catch (Exception e) {
		}
		try {
			int kr = Integer.parseInt(request.getParameter("kr"));
			rc.keyRelease(kr);
		} catch (Exception e) {
		}
		try {
			int mp = Integer.parseInt(request.getParameter("mp"));
			rc.mousePress(mp);
		} catch (Exception e) {
		}
		try {
			int mr = Integer.parseInt(request.getParameter("mr"));
			rc.mouseRelease(mr);
		} catch (Exception e) {
		}
		try {
			int mw = Integer.parseInt(request.getParameter("mw"));
			rc.mouseWheel(mw);
		} catch (Exception e) {
		}
		// ���ͼ��ҳ��
		response.setContentType("image/jpeg");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		ServletOutputStream out = response.getOutputStream();
		if(Boolean.parseBoolean(request.getParameter("img"))){
			ImageIO.write(image, "JPEG", out);
		}
		out.flush();
//		out.close();
		System.gc();
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
